﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.Xml;

namespace EntityLayer
{
   public class XmlHelper<T> where T : class, IObject, new()
    {
       string _FileName;
       public XmlHelper(string FileName)
       {
          // if (!File.Exists(FileName)) throw new FileNotFoundException();
           _FileName = FileName;
       }

       public List<T> Deserialise()
       {
           List<T> value = new List<T>();
           try
           {
               if (File.Exists(_FileName))
               {
                   XmlReader reader = new XmlTextReader(_FileName);
                   XmlSerializer serializer = new XmlSerializer(typeof(List<T>));
                   value = (List<T>)serializer.Deserialize(reader);
                   reader.Close();
               }
           }
           catch (Exception exc)
           {
               Console.WriteLine(exc.Message);
           }


           return value;
           
       }

       public void Serialise(List<T> value)
       {
           //if (File.Exists(_FileName)) File.WriteAllText(_FileName, "");
           using (var  writer = new FileStream(_FileName, FileMode.Create))
           {
               XmlSerializer serializer = new XmlSerializer(typeof(List<T>));
               serializer.Serialize(writer, value);
           }
       }



    }
}
